package com.xbongbong.paas.help.permission;

import com.xbongbong.paas.enums.DefaultPaasModulePermissionEnum;
import com.xbongbong.paas.pojo.PermissionComplexTermPojo;
import com.xbongbong.pro.enums.permission.enums.ProPermissionAliasEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.sys.domain.entity.ProPermissionEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限相关工具方法
 * @author kaka
 * @version v1.0
 * @date 2019/5/8 21:27
 * @since v1.0
 */
public class PermissionHelp {

    /**
     * 构造默认的Paas模块操作权限，List<PermissionComplexTermPojo>格式
     * @return 返回List<PermissionComplexTermPojo>格式的默认Paas模块操作权限
     */
    public static List<PermissionComplexTermPojo> initDefaultPaasModulePermission() {
        List<PermissionComplexTermPojo> modulePerms = new ArrayList<>();
        List<DefaultPaasModulePermissionEnum> modulePermEnums = DefaultPaasModulePermissionEnum.getAllEnum();
        PermissionComplexTermPojo termPojo;
        for (DefaultPaasModulePermissionEnum modulePermEnum : modulePermEnums) {
            termPojo = new PermissionComplexTermPojo();
            // 对于paas模块该参数无意义
            termPojo.setProPermissionId(-1);
            termPojo.setAlias(modulePermEnum.getAlias());
            termPojo.setName(modulePermEnum.getName());
            termPojo.setValue(modulePermEnum.getValue());
            modulePerms.add(termPojo);
        }
        return modulePerms;
    }

    /**
     * 构造默认的Saas模块操作权限，List<PermissionComplexTermPojo>格式
     * @param proPermission 传入的saas父模块ProPermissionEntity实体，需要已经构造好childList结构
     * @return 返回List<PermissionComplexTermPojo>格式的默认Saas模块操作权限
     */
    public static List<PermissionComplexTermPojo> initDefaultSaasModulePermission(ProPermissionEntity proPermission) {
        List<ProPermissionEntity> childList = proPermission.getChildList();
        return initDefaultSaasModulePermission(childList);
    }

    /**
     * 构造默认的Saas模块操作权限，List<PermissionComplexTermPojo>格式
     * @param childList 子权限列表
     * @return 返回List<PermissionComplexTermPojo>格式的默认Saas模块操作权限
     */
    public static List<PermissionComplexTermPojo> initDefaultSaasModulePermission(List<ProPermissionEntity> childList) {
        List<PermissionComplexTermPojo> modulePerms = new ArrayList<>();
        if (childList == null || childList.size() < 1) {
            return  modulePerms;
        }
        PermissionComplexTermPojo termPojo;
        for (ProPermissionEntity childPermission : childList) {
            termPojo = new PermissionComplexTermPojo();
            // 对于paas模块该参数无意义
            termPojo.setProPermissionId(childPermission.getId());
            termPojo.setAlias(childPermission.getAlias());
            termPojo.setName(childPermission.getName());
            termPojo.setValue(0);
            modulePerms.add(termPojo);
        }
        return modulePerms;
    }

    /**
     * 是否有客户和机会的查看权限
     * @param alias
     * @return
     */
    public static String hasViewPermission(String alias) {
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByAlias(alias);
        switch (xbbRefTypeEnum) {
            case CUSTOMER_MANAGEMENT:
            case CUSTOMER:
            case CUSTOMER_FOCUS:
            case CUSTOMER_DEAL:
            case CUSTOMER_FOLLOW:
            case CUSTOMER_NEARBY:
                alias = ProPermissionAliasEnum.CUSTOMER_VIEW.getAlias();
                break;
            case SALES_OPPORTUNITY:
            case ALL_OPPORTUNITY:
            case KEY_OPPORTUNITY:
                alias = ProPermissionAliasEnum.OPPORTUNITY_VIEW.getAlias();
                break;
            case PRODUCT:
                alias = ProPermissionAliasEnum.PRODUCT_VIEW.getAlias();
                break;
            case CLUE:
            case ALL_CLUE:
                alias = ProPermissionAliasEnum.CLUE_VIEW.getAlias();
                break;
            case WORK_ORDER_V2_APPOINT:

                alias = ProPermissionAliasEnum.WORK_ORDER_V2_ASSIGNED.getAlias();
                break;
            case WORK_ORDER_V2_POOL:
                alias = ProPermissionAliasEnum.WORK_ORDER_V2_POOL_VIEW.getAlias();
                break;
            default:
                break;
        }
        return alias;
    }
}
